<template>
  <div>
    <tablePaging :tableItem="tableItem" :operationItem="operationItem" />
  </div>
</template>
<script setup>
import tablePaging from '@/views/common/component/table-paging'
import { onMounted, reactive } from 'vue'
import {
  tableItem,
  operationItem
} from './const.js'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import {listDictItemByCode} from '@/api/config/dic'
import {editCourse} from "../../../../api/course";
const { proxy } = getCurrentInstance();
const router = useRouter()
const data = reactive({

})

function initTableItem() {
  let menuArray = operationItem.menuArray
  let addMenu = menuArray.find(item => item.code === 'add')
  addMenu.handle = goAdd

  let btnArray = tableItem.btnArray
  let detailBtn = btnArray.find(item => item.code === 'detail')
  detailBtn.handle = goDetail

  let editBtn = btnArray.find(item => item.code === 'edit')
  editBtn.handle = goEdit

  let publishedBtn = btnArray.find(item => item.code === 'published')
  publishedBtn.handle = (row)=>{
    publish(row, true)
  }
  let unpublishedBtn = btnArray.find(item => item.code === 'unpublished')
  unpublishedBtn.handle = (row)=>{
    publish(row, false)
  }
}

const initOptions = () => {
  let searchItemArray = operationItem.SearchItem
  let categoryItem = searchItemArray.find(item => item.code === 'categoryParentId')
  let kindItem = searchItemArray.find(item => item.code === 'kindId')

  listDictItemByCode({typeCode: 'courseTypeCode'}).then(res => {
    categoryItem.options = res.rows
  })

  listDictItemByCode({typeCode: 'courseKindCode'}).then(res => {
    kindItem.options = res.rows
  })

}

const goAdd = () =>{
  router.push({
    path: '/modules/course/course/save',
    query: {type: 'add'}
  })
}

const goEdit = (row) =>{
  router.push({
    path: '/modules/course/course/save',
    query: { id: row.id, type: 'edit'}
  })
}

const goDetail = (row) =>{
  router.push({
    path: '/modules/course/course/detail',
    query: { id: row.id}
  })
}

const publish = (row, isPublish) => {
  let requestData = {id: row.id}
  requestData.publishedFlag = isPublish ? 1 : 0
  editCourse(requestData).then(()=>{
    tableItem.pageInfo.paging()
  })
}

onMounted(() => {
  initTableItem()
  initOptions()
})
</script>

<style></style>
